<?php
/**
 * Created by PhpStorm.
 * @author : [zhuangze]
 * @Date: 18/8/15
 * @Time: 18:05
 */
namespace app\admin\model;

use app\common\model\Base;

class Admin extends Base {

    //超级管理员身份标识
    const SUPER_ADMIN_IDENTITY = '1';
    //删除状态
    const STATUS_DELETE_YES = 1;//已删除
    const STATUS_DELETE_NO = 0;//未删除


    /**
     * @description : [根据账号搜索账号]
     * @param $login_name
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\exception\DbException
     */
    public function findByLoginName($login_name){
        $where = [
            'login_name' => $login_name,
            'is_delete'  => self::STATUS_DELETE_NO,
        ];
        $admin = $this
            ->field($this->_getFieldList())
            ->where($where)
            ->find();
        return $admin;
    }

    /**
     * @description : [获取通用字段]
     */
    public function _getFieldList(){
        return [
            'id',
            'login_name',
            'login_pass_salt',
            'login_pass',
            'real_name',
            'mobile',
        ];
    }

    /**
     * @description : [验证登录密码]
     * @param     $password
     * @return  bool
     */
    public function validatePass($password){
        $password = $this->getAttr('login_pass_salt').$password ;
        $password_md5 = $this->getAttr('login_pass');
        if(md5($password)!=$password_md5) {
            return false;
        }
        return true;
    }

    /**
     * @description : [生成密码和密码盐]
     */
    public function generatePassAndSalt(){
        $login_pass_salt = generateRandStr();
        $this->setAttr('login_pass', md5($login_pass_salt.$this->getAttr('login_pass')));
        $this->setAttr('login_pass_salt', $login_pass_salt);
    }

}